package core.Resource
{
	import core.actor.core.BoneData;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class AniPack extends BasePack
	{
		
		public var data:Object;
		
		private var _loader:URLLoader;
		
		public function AniPack()
		{
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE,completeHandler);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, completeHandler);
		}
		
		public override function load(aniName:String,url:String):void
		{
			name = aniName;
			_loader.dataFormat = URLLoaderDataFormat.BINARY; // ** make sure you do this **
			_loader.load(new URLRequest(url));	
			state = 0;
		}
			
		
		private function completeHandler(evt:Event):void
		{
			if (evt.type == Event.COMPLETE)
			{
				var compressedData:ByteArray = URLLoader(evt.target).data;
				if (compressedData!=null)
				{
					try
					{
						ByteArray(compressedData).uncompress();
					}
					catch(e:Error)
					{
					}
				}
				var xmlData:XML = new XML(compressedData);
				data = BoneData.convert(xmlData);
				state = 1;
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
			else
			{
				throw new Error("TGP:Animation pack load error.Pack name:"+name);
			}
		}
		
		public override function reset():void
		{
			name = "";
			state = -1;
			if(data!=null)
				data = null;

		}
		
		public override function dispose():void
		{
			reset();
			if(_loader!=null)
			{
				_loader.removeEventListener(Event.COMPLETE,completeHandler);
				_loader.removeEventListener(IOErrorEvent.IO_ERROR, completeHandler);
				_loader = null
			}
		}
	}
}